/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.externalreference;

import java.util.ArrayList;

import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.externalreference.Classification;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class ClassificationIO extends
		AbstractEntityInstanceInputImpl<Classification> implements
		IEntityInstanceOutput<Classification> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Classification pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCCLASSIFICATION",
				appDataManager.getName(pojo));

		// Set Source
		if (pojo.getSource() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getSource(),
					StringParameter.UTF_8));
		}

		// Set Edition
		if (pojo.getEdition() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getEdition(),
					StringParameter.UTF_8));
		}

		// Set EditionDate
		if (pojo.getEditionDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getEditionDate()
					.toString(), StringParameter.UTF_8));
		}

		// Set Name
		ei.addParameter(new StringParameter(pojo.getName(),
				StringParameter.UTF_8));

		// Set Description
		if (pojo.getDescription() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getDescription(),
					StringParameter.UTF_8));
		}

		// Set Location
		if (pojo.getLocation() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLocation(),
					StringParameter.UTF_8));
		}

		// Set ReferenceTokens
		if (pojo.getReferenceTokens() == null
				|| pojo.getReferenceTokens().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter referenceTokens = new ListParameter();
			for (String str : pojo.getReferenceTokens()) {
				referenceTokens.add(new StringParameter(str,
						StringParameter.UTF_8));
			}
			ei.addParameter(referenceTokens);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Classification doCreateObject() {
		return new Classification();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			Classification pojo, IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			Classification pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Source
		Parameter source = sei.getParameter(0);
		if (source.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) source;
			pojo.setSource(s.toUTF8());
		}

		// Set Edition
		Parameter edition = sei.getParameter(1);
		if (edition.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) edition;
			pojo.setEdition(s.toUTF8());
		}

		// Set EditionDate
		Parameter editionDate = sei.getParameter(2);
		if (editionDate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) editionDate;
			pojo.setEditionDate(new Date(s.toUTF8()));
		}

		// Set Name
		StringParameter name = (StringParameter) sei.getParameter(3);
		pojo.setName(name.toUTF8());

		// Set Description
		Parameter description = sei.getParameter(4);
		if (description.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) description;
			pojo.setDescription(s.toUTF8());
		}

		// Set Location
		Parameter location = sei.getParameter(5);
		if (location.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) location;
			pojo.setLocation(s.toUTF8());
		}

		// Set ReferenceTokens
		Parameter referenceTokens = sei.getParameter(6);
		if (referenceTokens.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) referenceTokens;
			if (pojo.getReferenceTokens() == null) {
				pojo.setReferenceTokens(new ArrayList<String>());
			}
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				pojo.getReferenceTokens().add(s.toUTF8());
			}
		}
	}
}
